<?php

namespace App\Http\Admin\Controllers\System;

use App\Extend\Response;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as FrameController;
use Illuminate\Support\Facades\App;
use App\Http\Admin\Logics\LoginLogic;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Support\Str;

use Illuminate\Support\Facades\View;

class BaseController extends FrameController
{

	// 标题
	public $title = '';

	// 中间件
	//protected $middleware = [Auth::class];

	public function __construct()
	{
		$viewData = [
			//'username' => LoginLogic::getUsername(), //todo
			'title' => $this->title,
		];
		View::share($viewData);
	}

	public function getTemplateDefault($template='') {
		$path = request()->path();
		$pathArr = explode('/', $path);
		if($template) {
			array_pop($pathArr);
			$pathArr[] = $template;
		}
		$path = implode('.', $pathArr);
		$path = Str::snake($path);
		return $path;
	}

	public function fetch($data=[], $template = '') {
		return view($this->getTemplateDefault($template), $data);
	}
	
	public function setError($msg='失败', $code='500', $data=[]) {
		return response()->json(Response::setError($msg, $code, $data));
	}

	public function setSuccess($data=[], $count=0, $code=0) {
		if($data instanceof \App\Extend\ListWithCountStruct) {
			$count = $data->getCount();
			$data = $data->getList();
		}
		return response()->json(Response::setSuccess($data, $count, $code));
	}
}